package com.linln.modules.message.repository;

import com.linln.modules.message.domain.MessageRefund;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface MessageRefundRepository  extends JpaRepository<MessageRefund, Long> {

    /**
     * 阅读消息
     * @param refundId
     * @return
     */
    @Modifying
    @Transactional
    @Query( " UPDATE MessageRefund  SET  status = 1  WHERE  id = ?1 ")
    public Integer readMessage(Long refundId);

    /**
     * 查询所有未读信息
     * @return
     */
    @Modifying
    @Query(value = "SELECT * FROM message_refund WHERE  status =0 AND member_id =?1",nativeQuery=true)
    public List<MessageRefund> selectUnreadRefund(Long memeberId);

    /**
     * 查询所有信息  未读
     * @return
     */
    @Modifying
    @Query(value = "SELECT * FROM message_refund WHERE member_id =?1  and status = 0 ORDER BY create_date DESC ",nativeQuery=true)
    public List<MessageRefund> selectAllRefundNoRead(Long memeberId);

    /**
     * 查询所有信息  未读
     * @return
     */
    @Modifying
    @Query(value = "SELECT * FROM message_refund WHERE member_id =?1 and type=?2 and status = 0 ORDER BY create_date DESC ",nativeQuery=true)
    public List<MessageRefund> selectAllRefundNoRead(Long memeberId,Integer type);

    /**
     * 查询所有信息   已读
     * @return
     */
    @Modifying
    @Query(value = "SELECT * FROM message_refund WHERE member_id =?1  and status = 1  ORDER BY create_date DESC ",nativeQuery=true)
    public List<MessageRefund> selectAllRefundRead(Long memeberId);
    /**
     * 查询所有信息   已读
     * @return
     */
    @Modifying
    @Query(value = "SELECT * FROM message_refund WHERE member_id =?1 and type=?2 and status = 1  ORDER BY create_date DESC ",nativeQuery=true)
    public List<MessageRefund> selectAllRefundRead(Long memeberId,Integer type);

    @Modifying
    @Transactional
    @Query(value =  " insert INTO message_refund (create_date,update_date,message_id,member_id,admin_id,content,status,type) VALUES (now(),now(),?1,?2,?3,?4,?5,?6)",nativeQuery=true)
    public Integer saveRefund(Long messageId,Long memberId,Long adminId,String content,Integer status,Integer type);
}
